package com.ybkj.o2o.zhuanche.controller.v4;

import com.ybkj.o2o.zhuanche.amqp.DockingSuperviseApi;
import com.ybkj.o2o.zhuanche.common.api.DaijiaApi;
import com.ybkj.o2o.zhuanche.common.query.OrderQuery;
import com.ybkj.o2o.zhuanche.common.transport.Result;
import com.ybkj.o2o.zhuanche.entity.Car;
import com.ybkj.o2o.zhuanche.entity.CarType;
import com.ybkj.o2o.zhuanche.entity.Order;
import com.ybkj.o2o.zhuanche.entity.OrderMemo;
import com.ybkj.o2o.zhuanche.manager.CarManager;
import com.ybkj.o2o.zhuanche.manager.CarTypeManager;
import com.ybkj.o2o.zhuanche.manager.OrderManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("ZhuanCheOrderResv4")
@RequestMapping("api/orders/v4")
public class ZhuanCheOrderRes {

	@Autowired
	private OrderManager orderManager;

	@Autowired
	private CarTypeManager carTypeManager;
	
	@Autowired
	private CarManager carManager;
	
	@Autowired
	private DaijiaApi daijiaApi;

	@Autowired
	@Qualifier("dsRabbitTemplate")
	private RabbitTemplate dsRabbitTemplate;

	@Autowired
	private DockingSuperviseApi dockingSuperviseApi;

	@RequestMapping("countOrderNumByDriver")
	@ResponseBody
	public ResultTransport countOrderNumByDriver(@RequestParam("driverIds") Long[] driverIds,
			@RequestParam(required = false, value = "startTime") Long startTime, @RequestParam(required = false, value = "endTime") Long endTime,
			@RequestParam("appKey") String appKey) {

		ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
		try {

			Date start = null;
			Date end = null;

			if (null != startTime) {
				start = new Date(startTime);
			}
			if (null != endTime) {
				end = new Date(endTime);

			}

			List<Map<String, Object>> stringObjectMap = orderManager.countOrderNumByDriver(driverIds, start, end, appKey);
			instance.setData(stringObjectMap);

		} catch (Exception e) {
			e.printStackTrace();
			instance = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
		return instance;

	}

	/**
	 * 查询单个订单.
	 * 
	 * @param id
	 *            订单id
	 * @return 订单
	 */
	@ResponseBody
	@RequestMapping(value = "findByOrderId", method = RequestMethod.GET)
	public ResultTransport findByOrderId(@RequestParam("orderId") Long id, @RequestParam("appKey") String appKey) {
		if (StringUtils.isBlank(appKey) || null == id) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		Order order = orderManager.findByIdAndAppKey(id, appKey);
		if (null == order) {
			return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
		}

		try {

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("orderId", order.getId());
			map.put("orderNumber", order.getOrderNo());
			map.put("time", order.getServerTime().getTime());
			map.put("status", order.getStatus());
			map.put("startLng", order.getStartLng());
			map.put("startLat", order.getStartLat());
			map.put("endLng", order.getEndLng());
			map.put("endLat", order.getEndLat());
			map.put("fromPlace", order.getStartAddress());
			map.put("toPlace", order.getEndAddress());
			map.put("orderType", "zhuanche");
			map.put("passengerPhone", order.getPassengerPhone());
			map.put("passengerName", order.getPassengerName());
			map.put("companyAbbreviation", order.getCompanyName());
			map.put("makePrice", order.getMakePrice());
			map.put("isMakePrice", order.getIsMakePrice());

			StringBuilder builder = new StringBuilder();
			List<OrderMemo> list = order.getOrderMemos();
			if (list != null && list.size() != 0) {
				for (OrderMemo orderMemo : list) {
					builder.append(orderMemo.getMemo());
					builder.append(';');
				}
			}
			map.put("memo", builder.toString());

			map.put("areaId", order.getAreaId());// App上面没有此属性

			ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
			instance.setData(map);
			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
	}

	/**
	 * 查询车辆类型.
	 * 
	 * @param companyId
	 *            公司id
	 */

	@ResponseBody
	@RequestMapping(value = "queryType", method = RequestMethod.GET)
	public ResultTransport queryType(@RequestParam("companyId") Long companyId, @RequestParam("appKey") String appKey) {
		if (StringUtils.isBlank(appKey) || null == companyId) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		try {
			List<CarType> list = carTypeManager.findAll(companyId, appKey);
			ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
			List<Map<String, Object>> mapData = new ArrayList<Map<String, Object>>();
			if (list != null && list.size() != 0) {
				for (CarType carType : list) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", carType.getId());
					map.put("typeName", carType.getTypeName());
					mapData.add(map);
				}
			}
			instance.setData(mapData);
			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
	}

	/**
	 * 查询单个车型.
	 * 
	 * @param carTypeId
	 *            车型id
	 */

	@RequestMapping(value = "queryOneType", method = RequestMethod.GET)
	public @ResponseBody ResultTransport queryOneType(@RequestParam("carTypeId") Long carTypeId, @RequestParam("appKey") String appKey) {
		if (StringUtils.isBlank(appKey) || null == carTypeId) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		try {
			CarType carType = carTypeManager.findByCarTypeId(carTypeId, appKey);

			ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("carTypeId", carType.getId());
			map.put("areaId", carType.getAreaId());
			instance.setData(map);
			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
	}

	/**
	 * 抢单V4.
	 */

	@RequestMapping(value = "grab", method = RequestMethod.POST)
	public ResultTransport grab(@RequestParam("orderId") Long orderId, @RequestParam("employId") Long employId,
			@RequestParam("employName") String employName, @RequestParam("employPhone") String employPhone,
			@RequestParam("employCompanyId") Long employCompanyId, @RequestParam("employNo") String employNo,
			@RequestParam("employPhoto") String employPhoto, @RequestParam("employScore") Double employScore,
			@RequestParam(required = false, value = "rentalCompanyId") Long rentalCompanyId,
			@RequestParam(required = false, value = "rentalCompanyName") String rentalCompanyName, @RequestParam("operator") String operator,
			@RequestParam("appKey") String appKey) {
		if (null == orderId || null == employScore || null == employId || null == employCompanyId || StringUtils.isBlank(employNo)
				|| StringUtils.isBlank(employPhoto) || StringUtils.isBlank(operator) || StringUtils.isBlank(employPhone)
				|| StringUtils.isBlank(appKey) || StringUtils.isBlank(employName)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}

		Order order = orderManager.findByIdAndAppKey(orderId, appKey);
		if (null == order) {
			return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
		}

		if (order.getStatus() != 0) {
			return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_STATUS_ERROR);
		}

		try {
			Error error = orderManager.grab(orderId, employId, employName, employPhone, employCompanyId, employNo, employPhoto, employScore,
					rentalCompanyId, rentalCompanyName, operator, appKey);
			ResultTransport instance = ResultTransport.getErrorBy(error);
			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
	}

	/**
	 * 预约用车司机端-我来做.
	 */
	@RequestMapping(value = "yuyueDriverDo", method = RequestMethod.POST)
	public ResultTransport yuyueDriverDo(@RequestParam("orderId") Long orderId, @RequestParam(required = false, value = "employId") Long employId,
			@RequestParam(required = false, value = "serverUrl") String serverUrl,// 代驾项目服务器地址
			@RequestParam("appKey") String appKey) {
		if (null == orderId || StringUtils.isBlank(appKey)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		Order order = orderManager.findByIdAndAppKey(orderId, appKey);
		if (null == order) {
			return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
		}
		
		if (order.getStatus() > 1) {
			return ResultTransport.getErrorBy(ErrorConstantV4.YUYUE_HAD_ACCEPT);
		}
		
		ResultTransport instance = new ResultTransport();
		
		/**
		 *1、判断是否为预约订单
		 *2、通过订单中是否有employId判断该订单是通过后单派单还是司机在预约订单列表中主动接单 
		 *3、如果是司机主动接单则要在该订单中填充派单逻辑中的司机信息 ,并判断是否更新订单信息
		 */
		Integer isBook = order.getBookStatus();
		if (null == isBook || isBook.equals(0) ) {
			if (order.getStatus() != 1) {
				return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_STATUS_ERROR);
			}
			
			if (null != employId && null != order.getEmployId()) {
				if (!employId.equals(order.getEmployId())) {
					return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_EMPLOY_OWNER_ERROR);
				}
			}
			
			Map<String, Object> driverInfo = daijiaApi.queryDriver(serverUrl, employId);
			
			Integer driverStatus = (Integer) driverInfo.get("status");
			if (null != driverStatus && driverStatus > 1) {
				return ResultTransport.getErrorBy(ErrorConstantV4.YUYUE_CANT_ACCPET);
			}
			
		}else {
			
			Long employIdInOrder = order.getEmployId();
			if (null == employIdInOrder) {
				Map<String, Object> driverInfo = daijiaApi.queryDriver(serverUrl, employId);
				
//				Integer driverStatus = (Integer) driverInfo.get("status");
//				if (null != driverStatus && driverStatus > 1) {
//					return ResultTransport.getErrorBy(ErrorConstantV4.YUYUE_CANT_ACCPET);
//				}
				
				Long yuyueMax = daijiaApi.findYuyueMaxSetting(serverUrl);
				OrderQuery orderQuery = new OrderQuery();
				orderQuery.setAppKey(appKey);
				orderQuery.setBookStatus(1);
				orderQuery.setEmployId(employId);
				orderQuery.setStatusArray("0,1,2,3,4");
				Integer driverAllBookOrders = orderManager.findByParams(orderQuery).getRows().size();
				if (yuyueMax > 0 && driverAllBookOrders >= yuyueMax) {
					return ResultTransport.getErrorBy(ErrorConstantV4.YUYUE_MAX_ERROR);
				}else {
					instance.setCode(0);
					instance.setMessage("success");
				}
			}
		}
		return instance;
	}
	
	
	
	/**
	 * 接单V4.
	 */
	@RequestMapping(value = "jiedan", method = RequestMethod.POST)
	public ResultTransport jiedan(@RequestParam("orderId") Long orderId, @RequestParam(required = false, value = "employId") Long employId,
			@RequestParam(required = false, value = "serverUrl") String serverUrl,// 代驾项目服务器地址
			@RequestParam("appKey") String appKey) {
		if (null == orderId || StringUtils.isBlank(appKey)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		Order order = orderManager.findByIdAndAppKey(orderId, appKey);
		if (null == order) {
			return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
		}
		
		/**
		 *1、判断是否为预约订单
		 *2、通过订单中是否有employId判断该订单是通过后单派单还是司机在预约订单列表中主动接单 
		 *3、如果是司机主动接单则要在该订单中填充派单逻辑中的司机信息 ,并判断是否更新订单信息
		 */
		Integer isBook = order.getBookStatus();
		if (null == isBook || isBook.equals(0) ) {
			if (order.getStatus() != 1) {
				return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_STATUS_ERROR);
			}
			
			if (null != employId && null != order.getEmployId()) {
				if (!employId.equals(order.getEmployId())) {
					return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_EMPLOY_OWNER_ERROR);
				}
			}
		}else {
			
			Long employIdInOrder = order.getEmployId();
			if (null == employIdInOrder) {
				Map<String, Object> driverInfo = daijiaApi.queryDriver(serverUrl, employId);
				
//				Integer driverStatus = (Integer) driverInfo.get("status");
//				if (null != driverStatus && driverStatus > 1) {
//					return ResultTransport.getErrorBy(ErrorConstantV4.YUYUE_CANT_ACCPET);
//				}
				
				Car car = carManager.queryByEmployId(employId, appKey);
			    if (car == null) {
			      return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_HASBEEN_RUSHED);
			    }
			    order.setCarId(car.getId());
			    order.setCarNumber(car.getCarNumber());
				
				Long yuyueMax = daijiaApi.findYuyueMaxSetting(serverUrl);
				OrderQuery orderQuery = new OrderQuery();
				orderQuery.setAppKey(appKey);
				orderQuery.setBookStatus(1);
				orderQuery.setEmployId(employId);
				orderQuery.setStatusArray("0,1,2,3,4");
				Integer driverAllBookOrders = orderManager.findByParams(orderQuery).getRows().size();
				if (yuyueMax > 0 && driverAllBookOrders >= yuyueMax) {
					return ResultTransport.getErrorBy(ErrorConstantV4.YUYUE_MAX_ERROR);
				}
				
				order.setEmployId((Long) driverInfo.get("employId"));
				order.setEmployName((String) driverInfo.get("employName"));
				order.setEmployNo((String) driverInfo.get("employNo"));
				order.setEmployPhone((String) driverInfo.get("employPhone"));
				order.setEmployPhoto((String) driverInfo.get("employPhoto"));
				order.setEmployScore((Double) driverInfo.get("employScore"));
				order.setEmployCompanyId((Long) driverInfo.get("employCompanyId"));
				Integer isSuccess = orderManager.update(order);
				if (null == isSuccess) {
					return ResultTransport.getErrorBy(ErrorConstantV4.YUYUE_ORDER_ACCPET);
				}
			}
		}
		//-----------------------------------------------------------//
		
//		//是否能下预约订单逻辑判断
//		Date serverTime = order.getServerTime();
//		Date now = new Date();
//		if (null != isBook && isBook > 0) {
//			
//			Long yuyueTimes = daijiaApi.findYuyueTimeSetting(serverUrl);
//			Long yuyueMax = daijiaApi.findYuyueMaxSetting(serverUrl);
//			
//			OrderQuery orderQuery = new OrderQuery();
//			orderQuery.setAppKey(appKey);
//			orderQuery.setBookStatus(1);
//			orderQuery.setEmployId(employId);
//			orderQuery.setStatusArray("0,1,2,3,4");
//			Integer driverAllBookOrders = orderManager.findByParams(orderQuery).getRows().size();
//			
//			Long minutes = (serverTime.getTime() - now.getTime())/60000;
//			if (minutes >0 && minutes < yuyueTimes) {
//				return ResultTransport.getErrorBy(ErrorConstantV4.YUYUE_TIME_ERROR);
//			}
//			if (yuyueMax > 0 && driverAllBookOrders >= yuyueMax) {
//				return ResultTransport.getErrorBy(ErrorConstantV4.YUYUE_MAX_ERROR);
//			}
//		}
		//-----------------------------------------------------------//
		
		try {
			Error error = orderManager.jiedan(orderId, appKey);
			ResultTransport instance = ResultTransport.getErrorBy(error);
			if (error.getCode() == 0) {
				instance.setData(order);
			}
			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}

	}

	/**
	 * 拒单V4.
	 */

	@RequestMapping(value = "refuse", method = RequestMethod.POST)
	public ResultTransport refuse(@RequestParam("orderId") Long orderId, @RequestParam("employId") Long employId,
			@RequestParam("cause") String cause, @RequestParam("operator") String operator, @RequestParam("appKey") String appKey) {
		if (null == orderId || null == employId || StringUtils.isBlank(cause) || StringUtils.isBlank(operator) || StringUtils.isBlank(appKey)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		Order order = orderManager.findByIdAndAppKey(orderId, appKey);
		if (null == order) {
			return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
		}
		if (order.getStatus() != 1) {
			return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_STATUS_ERROR);
		}
		try {
			Error error = orderManager.refuse(orderId, employId, cause, operator, appKey);

			ResultTransport instance = ResultTransport.getErrorBy(error);
			if (error.getCode() == 0) {
				instance.setData(order);
			}
			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
	}

	/**
	 * 到达预约地,开始等待.
	 * 
	 * @param orderId
	 *            订单id
	 * @param operator
	 *            操作者
	 */

	@RequestMapping(value = "wait", method = RequestMethod.POST)
	public ResultTransport wait(@RequestParam("orderId") Long orderId, @RequestParam("operator") String operator,
			@RequestParam(required = false, value = "serverUrl") String serverUrl,
			@RequestParam("appKey") String appKey) {
		if (null == orderId || StringUtils.isBlank(operator) || StringUtils.isBlank(appKey)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		if (!orderManager.checkExist(orderId, appKey)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
		}
		Order order = orderManager.findByIdAndAppKey(orderId, appKey);
		
//		Integer isBook = order.getBookStatus();
//		if (null != isBook && isBook > 0) {
//			Long yuyueTime = daijiaApi.findZhuanYuyueSetting(serverUrl)/60000;
//			Date serverTime = order.getServerTime();
//			Date now = new Date();
//			Long remainMinutes = (serverTime.getTime() - now.getTime())/60000;
//			if (remainMinutes > 0 && yuyueTime > 0 && remainMinutes > yuyueTime) {
//				return ResultTransport.getErrorBy(ErrorConstantV4.YUYUE_CANT_WAIT);
//			}
//		}
		
		try {
			Error error = orderManager.waitV4(orderId, operator, appKey);

			ResultTransport instance = ResultTransport.getErrorBy(error);

			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
	}

	/**
	 * 开始执行订单.
	 * 
	 * @param orderId
	 *            订单id
	 * @param operator
	 *            操作者
	 * @param realStartAddress
	 *            真实上车地
	 * @param realStartLat
	 *            真实上车地纬度
	 * @param realStartLng
	 *            真实上车地经度
	 */

	@RequestMapping(value = "run", method = RequestMethod.POST)
	public ResultTransport run(@RequestParam("orderId") Long orderId, @RequestParam("operator") String operator,
			@RequestParam(required = false, value = "realStartAddress") String realStartAddress,// 真实上车地
			@RequestParam(required = false, value = "realStartLat") Double realStartLat,// 真实上车地纬度
			@RequestParam(required = false, value = "realStartLng") Double realStartLng,// 真实上车地经度
			@RequestParam("appKey") String appKey) {
		if (null == orderId || StringUtils.isBlank(appKey) || StringUtils.isBlank(operator)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		if (!orderManager.checkExist(orderId, appKey)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
		}
		try {
			Error error = orderManager.runV4(orderId, operator, appKey, realStartAddress, realStartLat, realStartLng);

			ResultTransport instance = ResultTransport.getErrorBy(error);

			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
	}

	/**
	 * 修改目的地.
	 * 
	 * @param orderId
	 *            订单id
	 * @param toPlace
	 *            目的地地址
	 * @param longitude
	 *            目的地经度
	 * @param latitude
	 *            目的地经度
	 */

	@ResponseBody
	@RequestMapping(value = "updateToPlace", method = RequestMethod.POST)
	public ResultTransport updateToPlace(@RequestParam("orderId") Long orderId, @RequestParam("toPlace") String toPlace,
			@RequestParam("longitude") Double longitude, @RequestParam("latitude") Double latitude, @RequestParam("appKey") String appKey) {
		if (null == orderId || StringUtils.isBlank(appKey)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		Order order = orderManager.findByIdAndAppKey(orderId, appKey);
		if (null == order) {
			return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
		}
		try {
			order.setEndAddress(toPlace);
			order.setEndLat(latitude);
			order.setEndLng(longitude);
			orderManager.update(order);
			ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);

			// 推送至监管平台
			dockingSuperviseApi.pushDdbgMessage(order, toPlace, latitude, longitude);

			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
	}

	/**
	 * 根据服务人员id查询已完成的金额大于等于指定金额的订单.
	 * 
	 * @param employId
	 *            服务人员id
	 * @param money
	 *            订单实际费用
	 * @param startTime
	 *            完成时间段
	 * @param endTime
	 *            完成时间段
	 * @return 订单集合
	 */

	@ResponseBody
	@RequestMapping(value = "findOrderByEmployId", method = RequestMethod.GET)
	public ResultTransport findOrderByEmployId(@RequestParam(required = true, value = "employId") Long employId,
			@RequestParam(required = true, value = "money") Double money, @RequestParam(required = true, value = "appKey") String appKey,
			@RequestParam(required = false, value = "startTime") Long startTime, @RequestParam(required = false, value = "endTime") Long endTime) {
		if (null == employId || money == null || StringUtils.isBlank(appKey)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		try {
			OrderQuery orderQuery = new OrderQuery();
			orderQuery.setAppKey(appKey);
			orderQuery.setEmployId(employId);
			orderQuery.setStatusArray("5");
			orderQuery.setRealPay(money);
			if (startTime != null) {
				orderQuery.setStartFinishTime(startTime);
			}
			if (endTime != null) {
				orderQuery.setEndFinishTime(endTime);
			}
			List<Order> list = orderManager.findOrderByEmployId(orderQuery);
			ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
			instance.setData(list);
			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
	}

	/**
	 * 报销订单.
	 */

	@RequestMapping(value = "baoxiao", method = RequestMethod.POST)
	public ResultTransport baoxiao(@RequestParam(required = true, value = "orderId") Long orderId,
			@RequestParam(required = true, value = "baoxiaoMoney") Double baoxiaoMoney, @RequestParam(required = true, value = "appKey") String appKey) {
		if (orderId == null || baoxiaoMoney == null || StringUtils.isBlank(appKey)) {
			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
		}
		try {
			if (!orderManager.checkExist(orderId, appKey)) {
				return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
			}
			orderManager.baoxiao(orderId, baoxiaoMoney, appKey);

			ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);

			return instance;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
		}
	}

	/**
	 * 服务人员销单.
	 */

	@RequestMapping(value = "cancel", method = RequestMethod.PUT)
	public ResponseEntity<Object> updateStatus(@RequestParam("orderId") Long orderId, @RequestParam("cause") String cause,
			@RequestParam("appKey") String appKey) {
		ResponseEntity<Object> responseEntity = null;
		appKey = StringUtils.trimToEmpty(appKey);
		try {
			Order order = orderManager.findByIdAndAppKey(orderId, appKey);
			orderManager.cancelByEmployee(orderId, cause, appKey);
			Result<Order> result = new Result<Order>(0, "成功", order);
			responseEntity = new ResponseEntity<Object>(result, HttpStatus.CREATED);
			return responseEntity;
		} catch (Exception ex) {
			ex.printStackTrace();
			Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
			responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
			return responseEntity;
		}
	} 
}
